const { organModel } = require('../models/organModel');
const { sectionModel } = require('../models/o_sectionModel');
const { typeModel } = require('../models/o_typeModel');

//查询 所有机构和关联的部门
async function getOrgans(req, res, next) {
    console.log(req.query);
    const { type,value } =req.query
    let organs = await organModel.find({ [type]:{ $regex:value, $options:'$i' } });
    let sections = await sectionModel.find();
    organs.push(...sections)
    // console.log(organs);
    // const organs = await organModel.aggregate([
    //     {
    //         // 做关联查询
    //         $lookup: {
    //             from: "organ_section",//需要连接的表名(不是模型名)
    //             localField: "_id",//本表需要关联的字段
    //             foreignField: "pid",//被连接表需要关联的字段
    //             as: "sectionData"//查询出的结果集别名
    //         }
    //     }
    // ])
    res.send(organs);
}

// 查询所有机构
async function getOrgan(req, res, next) {
    let organs = await organModel.find();
    res.send(organs)
}

// 通过id查询 部门数据
async function getSectionType(req, res, next) {
    console.log(req.query);
    // id为 0 的话 代表最上级机构----------
    if (req.query._id == '0') {
        res.send([{
            "_id": '001',
            "type": '分公司'
        }])
    } else {
        let sections = await typeModel.find();
        res.send(sections)
    }
}


// 创建最上级机构-----------------
async function createMaxOrgan(req, res, next) {
    console.log(req.query);
    let { organ_name, sel_section, organ_detail, sel_organ } = req.query;
    console.log(sel_organ);
    let result = await organModel.create({
        name: organ_name,
        type: sel_section,
        detail: organ_detail,
        pid: sel_organ
    })
    if (result) {
        res.send({
            code: 200,
            message: "新增成功"
        })
    }
}

async function createSection(req, res, next) {
    let { organ_name, sel_section, organ_detail, sel_organ } = req.query;
    let result = await sectionModel.create({
        name: organ_name,
        type: sel_section,
        detail: organ_detail,
        pid: sel_organ
    })
    if (result) {
        res.send({
            code: 200,
            message: "新增成功"
        })
    }
}

// 删除操作-----------------
async function delOrgan(req, res, next) {
    console.log(req.params);
    const result = await organModel.deleteOne({ _id: req.params });
    console.log(result);
    if (result.n == 0) {
        const sec = await sectionModel.deleteOne({ _id: req.params });
        if (sec.n > 0) {
            res.send({
                code: 200,
                message: "删除成功"
            })
        }
    } else {
        const sec = await sectionModel.deleteMany({ pid: req.params });
        res.send({
            code: 200,
            message: "删除最高机构成功,旗下机构全部删除"
        });
    }
}

//编辑操作----------------
async function updateOrgan(req, res, next) {
    const { update_name, update_detail, update_section } = req.body;
    console.log(req.params);
    const result = await organModel.updateOne(
        { _id: req.params },
        { name: update_name, detail: update_detail, type: update_section }
    )
    if (result.nModified == 0) {
        const sec = await sectionModel.updateOne(
            { _id: req.params },
            { name: update_name, detail: update_detail, type: update_section }
        )
        if (sec.nModified > 0) {
            res.send({
                code: 200,
                message: "修改成功"
            });
        }
    } else {
        res.send({
            code: 200,
            message: "修改成功"
        });
    }

}

module.exports = {
    getOrgans,
    getOrgan,
    getSectionType,

    createMaxOrgan,
    createSection,

    delOrgan,

    updateOrgan
}